package org.filetransfer.file_transfer_java_backend.repository;
import org.filetransfer.file_transfer_java_backend.entity.ShareItem;
import org.filetransfer.file_transfer_java_backend.model.ServiceShareInfoModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
@Repository
public interface ShareItemRepository extends JpaRepository<ShareItem, Long> {
    Optional<ShareItem> findByCode(String code);
    boolean existsByCode(String code);
    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceShareInfoModel(" +
            "count(s)" +
            ") from ShareItem s")
    Optional<ServiceShareInfoModel> getStatistics();
    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceShareInfoModel(" +
            "count(s)" +
            ") from ShareItem s where s.createTime >= :start and s.createTime <= :end")
    Optional<ServiceShareInfoModel> getStatisticsInRange(Instant start, Instant end);
    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceShareInfoModel(" +
            "count(s)" +
            ") from ShareItem s where s.user.id = :userId")
    Optional<ServiceShareInfoModel> getStatisticsByUser(Long userId);
    @Query("select new org.filetransfer.file_transfer_java_backend.model.ServiceShareInfoModel(" +
            "count(s)" +
            ") from ShareItem s where s.createTime >= :start and s.createTime <= :end and s.user.id = :userId")
    Optional<ServiceShareInfoModel> getStatisticsInRangeByUser(Instant start, Instant end, Long userId);
    // 根据用户ID查找分享列表，按创建时间倒序排列
    List<ShareItem> findByUserIdOrderByCreateTimeDesc(Long userId);
}
